﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.Cooperation.WebsiteCooperation;
using BCData.Common.Cooperation.WebsiteCooperationFollowUp;
using BCDto.Common.Cooperation.WebsiteCooperationFollowUp;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.Cooperation.WebsiteCooperationFollowUp;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.Common.Cooperation.WebsiteCooperationFollowUp
{
    public class WebsiteCooperationFollowUpService : IWebsiteCooperationFollowUpService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWebsiteCooperationData websiteCooperationData;
        private readonly IWebsiteCooperationFollowUpData websiteCooperationFollowUpData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteCooperationFollowUpEntity);

        public WebsiteCooperationFollowUpService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWebsiteCooperationData websiteCooperationData,
            IWebsiteCooperationFollowUpData websiteCooperationFollowUpData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.websiteCooperationData = websiteCooperationData;
            this.websiteCooperationFollowUpData = websiteCooperationFollowUpData;
        }

        #region 检查官网合作留言跟进记录是否存在
        public bool Exists(int websiteCooperationFollowUpId)
        {
            return this.websiteCooperationFollowUpData.Exists(websiteCooperationFollowUpId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加官网合作留言跟进记录
        public WebsiteCooperationFollowUpDto Add(WebsiteCooperationFollowUpRequestDto requestDto,AdminDto adminDto)
        {
            if (!websiteCooperationData.Exists(requestDto.WebsiteCooperationId).Result)
            {
                throw new ArgumentException("此合作留言不存在");
            }
            databaseContext.BeginTransaction();
            var cooperationEntity = websiteCooperationData.GetEntity(requestDto.WebsiteCooperationId).Result;
            var entity = requestDto.As<WebsiteCooperationFollowUpEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;
            entity.WebsiteCooperationId = cooperationEntity.WebsiteCooperationId;
            entity.Company = cooperationEntity.Company;
            var result = this.websiteCooperationFollowUpData.Add(entity).Result;
            if (result != null)
            {
                cooperationEntity.HasFollowUp = true;
                cooperationEntity.FollowUpTimes++;
                var cooperation = this.websiteCooperationData.Update(cooperationEntity).Result;
            }
            databaseContext.Commit();
            return result.As<WebsiteCooperationFollowUpDto>();
        }
        #endregion
        
        #region 删除官网合作留言跟进记录
        public bool Delete(int websiteCooperationFollowUpId)
        {
            if (!this.Exists(websiteCooperationFollowUpId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.websiteCooperationFollowUpData.Delete(websiteCooperationFollowUpId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteCooperationFollowUpId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取官网合作留言跟进记录
        public WebsiteCooperationFollowUpDto Get(int websiteCooperationFollowUpId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteCooperationFollowUpId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteCooperationFollowUpData.GetEntity(websiteCooperationFollowUpId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteCooperationFollowUpDto>();
        }
        #endregion

        #region 获取官网合作留言跟进记录分页
        public PageableList<WebsiteCooperationFollowUpDto> Query(int? websiteCooperationId,DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, websiteCooperationId, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteCooperationFollowUpData.Query(websiteCooperationId, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WebsiteCooperationFollowUpDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WebsiteCooperationFollowUpDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}